//
//  main.cpp
//  Prism
//
//  Created by Bas du Pre on 21-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include <iostream>
#include <cstdlib>

#include "Prism.h"

using namespace Prism$;

PFloat speed = 100.0; // 60 pixel per second

class Main : public PObject {
public:
    void update(void* sender, UpdateEventArgs *e) {
        //NSLog(@"Update!");
        Sprite *sprite = (Sprite*)sender;
        sprite->setPosition(sprite->position().x - 1 * (speed * e->delta), sprite->position().y);
        sprite->setRotation(sprite->rotation() + 0.8 * (speed * e->delta));
        //sprite->setScale(sprite->scale() + 0.002 * (speed * e->delta));
        if (sprite->position().x < -100)
            sprite->setPosition(theDirector()->winSize().width + 100, sprite->position().y);
    }
    void update2(void* sender, UpdateEventArgs *e) {
        //NSLog(@"Update!");
        Sprite *sprite = (Sprite*)sender;
        //sprite->setPosition(sprite->position().x - 2 * (speed * e->delta), sprite->position().y);
        sprite->setRotation(sprite->rotation() - 2 * (speed * e->delta));
        if (sprite->position().x < -100)
            sprite->setPosition(theDirector()->winSize().width + 100, sprite->position().y);
    }

    void walkAnimation(void* sender, UpdateEventArgs *e) {
        //NSLog(@"Update!");
        Sprite *sprite = (Sprite*)sender;
        sprite->setPosition(sprite->position().x + 0.36 * (speed * e->delta), sprite->position().y);
        //sprite->setRotation(sprite->rotation() + 1 * (speed * e->delta));
        if (sprite->position().x > theDirector()->winSize().width + 24)
            sprite->setPosition(-24, sprite->position().y);
    }
    void updateFont(void* sender, UpdateEventArgs *e) {
        Label *label = (Label*)sender;
        label->setTag(label->tag()+1);
        
        if (label->tag() >= 60) {
            label->setSize(label->size() + 4);
            label->setTag(0);
            label->setValue(PFloat(theDirector()->fps()));
        }
    }
    
    void onTap(void* sender, TouchEventArgs *e) {
        PRISM_LOGINFO_QUIET("I have been touched!");
    }
    
    void onTouchesBegan(void* sender, TouchesEventArgs *e) {
        PRISM_LOGINFO_QUIET("onTouchesBegan: e.touches.length = %d", e->touches.length());
    }
};

Main *m;
void prism_initialize() {   
    m = new Main();
        
    Scene *scene = new Scene();
    
    #if 0
    int num = 100;
    int u = 0;
    for (int i = 0; i < num; i++, u=((u+1)%3)) {
        Sprite *sprite = new Sprite(File::resourcesPath() + "/Icon.png");
        sprite->setPosition((rand()%(theDirector()->winSize().width + 200)) + 100, (rand()%(theDirector()->winSize().height)));    
        sprite->setAnchorPoint(0.5, 0.5);
        scene->addChild(sprite, -1);
        if (u == 0) sprite->onUpdate += delegate(m, &Main::update);
        if (u == 1) sprite->onUpdate += delegate(m, &Main::update2);
        if (u == 2) sprite->onUpdate += delegate(m, &Main::update);
    }
    #endif
        
    PRISM_LOG(File::resourcesPath());
        
    Sprite *sprite = new Sprite(File::resourcesPath() + "/Icon.png");
    sprite->setPosition(theDirector()->winSize().width - 100, 300);    
    
    sprite->onTap += delegate(m, &Main::onTap);
    sprite->onTouchesBegan += delegate(m, &Main::onTouchesBegan);
    scene->onTouchesBegan += delegate(m, &Main::onTouchesBegan);
    
    Sprite *sprite2 = new Sprite(File::resourcesPath() + "/Icon-72.png");
    sprite2->setPosition(40, 100);    
    
    Sprite *sprite3 = new Sprite(File::resourcesPath() + "/bas2.png");
    sprite3->setPosition(10, 100);    
    
    sprite->onUpdate += delegate(m, &Main::update);
    sprite2->onUpdate += delegate(m, &Main::update2);
    //sprite3->onUpdate += delegate(m, &Main::update3);
    
    scene->addChild(sprite, 7);
    sprite->addChild(sprite2, 1);
    sprite2->addChild(sprite3, 2);
    
    //sprite->setScale(0.5);
    
    
    List<String> bla;
    bla.pushBack("hoi");
    bla.pushBack("hallo");
    bla.pushBack("goedendag");
    bla.pushBack("halloo!");
    PRISM_LOG("%@", &bla);
     
    bla.removeAll();
     
    
    Label *label = new Label("Prism test", "Arial", 20, Color::Lime, Label::QualitySpeed);
    label->setPosition(100, 100);
    label->setAnchorPoint(0, 0);
    //label->onUpdate += delegate(m, &Main::updateFont);
    //scene->addChild(label, 10);
    label->color().setAlpha(50);
    
    Label *label2 = new Label("Prism test", "Arial", 20, Color::Red, Label::QualityHigh);
    label2->setPosition(100, 190);
    sprite->addChild(label2, 10);
    //label2->onUpdate += delegate(m, &Main::updateFont);
    
    Animation *a = new Animation();
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(0, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(24, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(72, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(48, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(0, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(96, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(144, 0, 24, 24)));
    a->addFrame(new TextureFrame(new Texture(File::resourcesPath() + "/body.png"), Prism::Rect(120, 0, 24, 24)));
    a->setDelay(0.07);   
    
    Sprite *mannetje = new Sprite(a);
    mannetje->setPosition(40, 200);   
    mannetje->texture()->setAntiAlias(false);
    scene->addChild(mannetje);
    
    Sprite *hoofdje = new Sprite(File::resourcesPath() + "/head2.png");
    hoofdje->setPosition(-1, -18);
    mannetje->addChild(hoofdje, -1);
    hoofdje->texture()->setAntiAlias(false);
    
    mannetje->setScale(1);
    mannetje->onUpdate += delegate(m, &Main::walkAnimation);
     
    
    /*theDirector()->setShowDebugLayer(true);
    Texture *t = new Texture("preview2.jpg");
    Scene *scene = new Scene();
    Sprite *s = new Sprite(t);
    s->setPosition(0, 0);
    s->setAnchorPoint(0, 0);
    s->setScale(0.5);
    scene->addChild(s);*/
    theDirector()->runWithScene(scene);
}

void prism_terminate() {
    //delete m;
}
